﻿Imports System.Data.SqlClient
Public Class Books
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Request.Browser.IsMobileDevice Then
            Response.Redirect("~/Books_mobile.aspx")
        End If
        'Assign text to the heading
        myHeading.InnerHtml = "List of Books"

        If IsPostBack = False Then
            tb_search.Attributes.Add("x-webkit-speech", "x-webkit-speech")
            'Populate the tag cloud
            TagCloud.getTagCloud(SqlDataSource1, CloudTags, Page)
            LanguageCloud.getLanguageCloud(SqlDataSource3, LanguageTags, Page)

            Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("BooksReviewDBConnectionString1").ConnectionString.ToString)
            Dim SqlString As String = "INSERT INTO Site_Activity(Activity) VALUES (@f1)"
            Dim cmd As SqlCommand = New SqlCommand(SqlString, conn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@f1", "Book List")
            conn.Open()
            cmd.ExecuteNonQuery()
            conn.Close()
        End If
    End Sub

    Protected Sub SqlDataSource2_Selected(sender As Object, e As System.Web.UI.WebControls.SqlDataSourceStatusEventArgs) Handles SqlDataSource2.Selected
        'If datasource is empty
        If (e.AffectedRows < 1) Then
            myHeading.InnerHtml = "No Books found"
        End If
        If (e.AffectedRows < 13) Then
            DataPagerBooks.Visible = False
        Else
            DataPagerBooks.Visible = True
        End If
    End Sub
    Protected Sub ListView1_DataBound(sender As Object, e As EventArgs)
        'Display datapager only if there is more than one page to display
        DataPagerBooks.Visible = (DataPagerBooks.PageSize < DataPagerBooks.TotalRowCount)
    End Sub

    Protected Sub btn_findByName_Click(sender As Object, e As EventArgs) Handles btn_findByName.Click
        searchDatabase("BookName", tb_search.Text)
    End Sub

    Protected Sub btn_findByRegion_Click(sender As Object, e As EventArgs) Handles btn_findByCategory.Click
        searchDatabase("Category", tb_search.Text)
    End Sub

    Private Function searchDatabase(ByVal buttonStr As String, ByVal searchStr As String)
        SqlDataSource2.SelectCommand = "SELECT * FROM Books_Complete WHERE " & buttonStr & " LIKE '%" & searchStr & "%' AND IsVerified = 1 ORDER BY DateAdded DESC"
        SqlDataSource2.DataBind()
        Return True
    End Function

    Protected Function getSlug(ByVal Title As String) As String
        Dim Slug As String = Title.ToLower()
        ' Replace - with empty space
        Slug = Slug.Replace("-", " ")
        ' Replace unwanted characters with space
        Slug = Regex.Replace(Slug, "[^a-z0-9\s-]", " ")
        ' Replace multple white spaces with single space
        Slug = Regex.Replace(Slug, "\s+", " ").Trim()
        ' Replace white space with -
        Slug = Slug.Replace(" ", "-")
        Return Slug
    End Function
End Class